Person = function(ctx,image,name,fullname,y,x,player,gameloop){

	this.ctx = ctx;
	this.images = preloader.images;
	this.image = image;
	this.name = name;
	this.fullname = fullname;
	this.needsOrder = false;
	this.orderTimeout = null;
	this.layout = null;
	this.storyCounter = 0;
	this.story = null;
	this.lastStory = '';
	this.talkActive = false;
	this.talkTimeout = null;
	this.$tc = null;
	this.$storyName = null;
	this.$greeting = null;
	this.$storyText = null;
	this.$bye = null;
	this.$space = null;
	this.$sorry = null;
	this.$end = null;
	this.$person = null;
	this.metMe = false;
	this.talkedCount = 0;
	this.stories = null;


	this.gameloop = gameloop;

	this.player = player;

	this.position = {
		'y':y,
		'x':x
	};

	this.init = function(stories){
		this.stories = stories;
		
		this.orderTimeout = window.setTimeout(function(){
			this.needsOrder = true;
		}.bind(this),1500);

		this.$tc = $('#talk_'+this.name);
		this.$storyName =  this.$tc.find('.storyName');
		this.$greeting =  this.$tc.find('.greeting');
		this.$storyText = this.$tc.find('.storyText');
		this.$bye =  this.$tc.find('.bye');
		this.$space =  this.$tc.find('.space');
		this.$sorry =  this.$tc.find('.sorry');
		this.$end = this.$tc.find('.end');
		this.$person = this.$tc.find('.person');

	};

	this.loop = function(layout){
		var px = this.player.position.x,
			py = this.player.position.y,
			x = this.position.x,
			y = this.position.y;

		if(		x+1 >= px
			&&	x-1 <= px
			&&	y+1 >= py
			&&  y-1 <= py ) {
			if( x == px && y == py && this.metMe == false) {
				if( this.talkActive == false ) {
					this.talk();
					this.metMe = true;
				}
			}

			if( this.needsOrder ) {
				//layout = this.walk(layout);
			}
		}
		
		if( x != px || y != py ){
			if( this.talkActive ) {
				window.clearTimeout(this.talkTimeout);
				$('#talk_'+this.name).fadeOut();
				$('#talksBackground').fadeOut();
				this.talkActive = false;
			}
			this.metMe = false;
		}

		this.layout = layout;

		return layout;

	};
	
	this.talk = function(){

		var $journal = null;

		this.talkActive = true;

		if( this.talkedCount < 4 ) {

			if( this.story == null ) {
				this.story = this.stories.getStory(this.lastStory,this);
				this.story.next.push(this.fullname);
			}

			this.$storyText.hide();
			this.$bye.hide();
			this.$space.hide();

			this.$storyName.html(this.story.name).css('color',this.story.color);
			this.$storyText.html(this.story.getText()).css('color',this.story.color);
			this.$person.html(this.fullname+':');


			if( this.story.counter >= 7 ) {

				$journal = $('#journal_'+this.story.name.replace(/ /g,"").toLowerCase())

				$('.journal').fadeOut();
				$journal.fadeIn();

				mainLoop.pause();

				this.$tc.fadeIn('fast',function(){
					this.$storyText.fadeIn(2000,function(){
						this.$end.fadeIn(2000);
					}.bind(this));
				}.bind(this));
				

				$('#talksBackground').fadeTo(10000,0.99,function(){
						$('#end').fadeIn(10000);
				}.bind(this.$tc));

				return;


			}

			this.$tc.fadeIn();

			this.talkTimeout = window.setTimeout(function(){

				var next = null,
					$journal = $('#journal_'+this.story.name.replace(/ /g,"").toLowerCase());
					
				mainLoop.pause();
				
				$('#talksBackground').fadeTo('slow',0.7);

				closeHelp();

				this.$storyText.slideDown();

				$('.journal').fadeOut();
				$journal.append('<div>'+this.$storyText.html()+'</div>').css('color',this.story.color).fadeIn();

				this.talkTimeout = window.setTimeout(function(){
					this.$bye.slideDown();
					this.$space.slideDown();
					//$('#talksBackground').fadeOut('slow');
					mainLoop.start();

				}.bind(this),2000);

				this.talkedCount += 1;
				this.lastStory = this.story.name;
				next = this.stories.told(this);

				this.$bye.html('Ask '+ next +' for more informations.');
				mainLoop.activeWindow = this.$tc;

			}.bind(this),1000);
		} else {

			this.$storyName.hide();
			this.$greeting.hide();
			this.$storyText.hide();
			this.$bye.hide();
			this.$tc.fadeIn();
			this.$sorry.show();
			this.$space.show();
			mainLoop.activeWindow = this.$tc;

		}
		
	}
	
	this.walk = function(layout){

		// prevent stepping over people who are already there
		if( typeof layout[1][1].person != 'undefined' ) {
			return layout;
		}
		
		// Go right
		if( this.player.position.x > this.position.x) {
			
			// under player
			if( this.player.position.y > this.position.y ) {
				if( layout[2][0] != null && typeof layout[2][1].person == 'undefined' && layout[2][0].e !== null ) {
					this.position.x -= 1;
					layout[2][1].person = layout[2][0].person;
					delete layout[2][0].person;
				}
			}
			//besides player
			if( this.player.position.y == this.position.y ) {
				if( layout[1][0] != null && typeof layout[1][1].person == 'undefined' && layout[1][0].e !== null ) {
					this.position.x -= 1;
					layout[1][1].person = layout[1][0].person;
					delete layout[1][0].person;
				}
			}
			//over player
			if( this.player.position.y < this.position.y ) {
				if( layout[0][0] != null && typeof layout[0][1].person == 'undefined' && layout[0][0].e !== null ) {
					this.position.x -= 1;
					layout[0][1].person = layout[0][0].person;
					delete layout[0][0].person;
				}
			}
		
		}
		
		// Go left
		if( this.player.position.x < this.position.x) {
			
			// under player
			if( this.player.position.y > this.position.y ) {
				if( layout[2][1] != null && typeof layout[2][1].person == 'undefined' && layout[2][1].w !== null ) {
					this.position.x += 1;
					layout[2][1].person = layout[2][2].person;
					delete layout[2][2].person;
				}
			}
			//besides player
			if( this.player.position.y == this.position.y ) {
				if( layout[1][1] != null && typeof layout[1][1].person == 'undefined' && layout[1][1].w !== null ) {
					this.position.x += 1;
					layout[1][1].person = layout[1][0].person;
					delete layout[1][0].person;
				}
			}
			//over player
			if( this.player.position.y < this.position.y ) {
				if( layout[0][1] != null && typeof layout[0][1].person == 'undefined' && layout[0][1].w !== null ) {
					this.position.x += 1;
					layout[0][1].person = layout[0][2].person;
					delete layout[0][2].person;
				}
			}
		
		}
		
		// Go up
		if( this.player.position.y < this.position.y ) {
			
			// right of player
			if( this.player.position.x > this.position.x ) {
				if( layout[2][2] != null && typeof layout[1][2].person == 'undefined' && layout[2][2].n !== null ) {
					this.position.y -= 1;
					layout[2][1].person = layout[2][2].person;
					delete layout[2][2].person;
				}
			}
			// under player
			if( this.player.position.x == this.position.x ) {
				if( layout[2][1] != null && typeof layout[1][1].person == 'undefined' && layout[2][1].n !== null ) {
					this.position.y -= 1;
					layout[1][1].person = layout[2][1].person;
					delete layout[2][1].person;
				}
			}
			// left of player
			if( this.player.position.x < this.position.x ) {
				if( layout[2][0] != null && typeof layout[1][0].person == 'undefined' && layout[2][0].n !== null ) {
					this.position.y -= 1;
					layout[1][0].person = layout[2][0].person;
					delete layout[2][0].person;
				}
			}
		
		}
		
		// Go down
		if( this.player.position.y > this.position.y ) {
			// right of player
			if( this.player.position.x > this.position.x ) {
				if( layout[0][2] != null && typeof layout[1][2].person == 'undefined' && layout[0][2].s !== null ) {
					this.position.y += 1;
					layout[1][2].person = layout[2][2].person;
					delete layout[2][2].person;
				}
			}
			// over player
			if( this.player.position.x == this.position.x ) {
				if( layout[0][1] != null && typeof layout[1][1].person == 'undefined' && layout[0][1].s !== null ) {
					this.position.y += 1;
					layout[1][1].person = layout[0][1].person;
					delete layout[0][1].person;
				}
			}
			// left of player
			if( this.player.position.x < this.position.x ) {
				if( layout[0][0] != null && typeof layout[1][0].person == 'undefined' && layout[0][0].s !== null ) {
					this.position.y += 1;
					layout[0][1].person = layout[0][0].person;
					delete layout[0][0].person;
				}
			}
		
		}
		
		this.needsOrder = false;

		this.orderTimeout = window.setTimeout(function(){
			this.needsOrder = true
		}.bind(this),1500);

		return layout;

		
	}


 
}